package com.nsgf.library.ui.product;

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.kunminx.architecture.ui.page.DataBindingConfig;
import com.kunminx.architecture.ui.state.State;
import com.kunminx.binding_recyclerview.adapter.BaseDataBindingAdapter;
import com.kunminx.binding_recyclerview.adapter.SimpleDataBindingAdapter;
import com.zhht.htzx.BR;
import com.zhht.htzx.R;
import com.nsgf.library.data.bean.ProductBean;
import com.nsgf.library.data.bean.ProductBean;
import com.nsgf.library.data.response.ResponseStatus;
import com.zhht.htzx.databinding.ActivityNewComersBinding;
import com.zhht.htzx.databinding.ItemNewComersBinding;
import com.nsgf.library.domain.request.AccountRequester;
import com.nsgf.library.ui.page.BaseActivity;
import com.nsgf.library.ui.page.StateHolder;

import java.util.ArrayList;
import java.util.List;


/**
 * 新人专享
 */
public class NewComersActivity extends BaseActivity {

    private NewComersStates mStates;

    private SimpleDataBindingAdapter<ProductBean, ItemNewComersBinding> mAdapter;

    private AccountRequester mAccountRequester;
    @Override
    protected void initViewModel() {
        mStates = new NewComersStates();
        mAccountRequester = new AccountRequester();
    }

    @Override
    protected DataBindingConfig getDataBindingConfig() {
        mAdapter = new SimpleDataBindingAdapter<ProductBean, ItemNewComersBinding>(this, R.layout.item_new_comers, new DiffUtil.ItemCallback<ProductBean>() {
            @Override
            public boolean areItemsTheSame(@NonNull ProductBean oldItem, @NonNull ProductBean newItem) {
                return false;
            }

            @Override
            public boolean areContentsTheSame(@NonNull ProductBean oldItem, @NonNull ProductBean newItem) {
                return false;
            }
        }) {
            @Override
            protected void onBindItem(ItemNewComersBinding binding, ProductBean item, RecyclerView.ViewHolder holder) {
                binding.setVm(item);
            }
        };
        return new DataBindingConfig(R.layout.activity_new_comers, BR.vm, mStates)
                .addBindingParam(BR.adapter,mAdapter);
    }

    private ActivityNewComersBinding mBinding;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        getLifecycle().addObserver(mAccountRequester);
        mStates.titleBar.set("新人专享");

        mAccountRequester.queryDiscountedProducts(50);
        mBinding = (ActivityNewComersBinding) getBinding();
        mBinding.recyclerView.setItemAnimator(null);
        mAccountRequester.getQueryDiscountedProductsResult().observe(this, productBeanResponseStatus -> {
            if (productBeanResponseStatus.getResponseCode() == 0) {
                mStates.list.set(productBeanResponseStatus.getData().getList());
                mAdapter.notifyDataSetChanged();
            }
        });

        mAdapter.notifyDataSetChanged();


        mAdapter.setOnItemClickListener((viewId, item, position) -> startActivity(new Intent()
                .putExtra("id",item.getSpuId())
                .setClass(NewComersActivity.this, ProductDetailActivity.class)));


    }
    public static class NewComersStates extends StateHolder {

        public final State<List<ProductBean>> list = new State<>(new ArrayList<>());
    }
}
